package com.gomore.opple.module.account.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.gomore.opple.R;
import com.gomore.opple.model.ShippingAddress;
import com.gomore.opple.module.IntentStart;
import com.gomore.opple.web.cgform.goods.view.VOResellerGoodsEntity;
import com.gomore.opple.web.cgform.shippingaddr.entity.TOShippingAddrEntity;
import com.gomore.opple.web.cgform.shopcart.entity.TOShopcartEntity;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by asus on 2017/5/18.
 */
public class AddressAdapter extends BaseAdapter {
    Activity mactivity;
    Context mcontext;
    List<ShippingAddress> shippingAddresses;

    public AddressAdapter(Activity activity, Context context, List<ShippingAddress> data) {
        mactivity = activity;
        mcontext = context;
        shippingAddresses = data;
    }

    @Override
    public int getCount() {
        return shippingAddresses.size();
    }

    @Override
    public Object getItem(int position) {
        return shippingAddresses == null ? 0 : shippingAddresses.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.address_item, parent, false);
            viewHolder.item_click = (LinearLayout) convertView.findViewById(R.id.item_click);
            viewHolder.image_box = (CheckBox) convertView.findViewById(R.id.image_box);
            viewHolder.consumer_name = (TextView) convertView.findViewById(R.id.consumer_name);
            viewHolder.consumer_mobile = (TextView) convertView.findViewById(R.id.consumer_mobile);
            viewHolder.consumer_address = (TextView) convertView.findViewById(R.id.consumer_address);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final ShippingAddress shippingAddress = shippingAddresses.get(position);
        if (shippingAddress != null) {
            if (shippingAddress.isSelect()) {
                viewHolder.image_box.setChecked(true);
            } else {
                viewHolder.image_box.setChecked(false);
            }
            if (shippingAddress.getName() != null) {
                viewHolder.consumer_name.setText(shippingAddress.getName());
            }
            if (shippingAddress.getMiblephone() != null) {
                viewHolder.consumer_mobile.setText(shippingAddress.getMiblephone());
            }
            if (shippingAddress.getAddress() != null) {
                viewHolder.consumer_address.setText(shippingAddress.getAddress());
            }
        }
        viewHolder.item_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentStart.getInstance().startAddAddressActivity(mactivity, trans(shippingAddress));
            }
        });

        viewHolder.image_box.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox checkBox = (CheckBox) view;
                if (checkBox.isChecked()) {
                    setSelect(shippingAddresses, position, true);
                } else {
                    setSelect(shippingAddresses, position, false);
                }
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    class ViewHolder {
        LinearLayout item_click;
        CheckBox image_box;
        TextView consumer_name, consumer_mobile, consumer_address;
    }

    private TOShippingAddrEntity trans(ShippingAddress shippingAddress) {
        TOShippingAddrEntity toShippingAddrEntity = new TOShippingAddrEntity();
        toShippingAddrEntity.setId(shippingAddress.getId());
        toShippingAddrEntity.setCreateDate(shippingAddress.getCreateDate());
        toShippingAddrEntity.setSysOrgCode(shippingAddress.getSysOrgCode());
        toShippingAddrEntity.setName(shippingAddress.getName());
        toShippingAddrEntity.setMiblephone(shippingAddress.getMiblephone());
        toShippingAddrEntity.setProvince(shippingAddress.getProvince());
        toShippingAddrEntity.setCity(shippingAddress.getCity());
        toShippingAddrEntity.setCountry(shippingAddress.getCountry());
        toShippingAddrEntity.setAddress(shippingAddress.getAddress());
        toShippingAddrEntity.setConsumerId(shippingAddress.getConsumerId());
        return toShippingAddrEntity;
    }

    private void setSelect(List<ShippingAddress> shippingAddresses, int position, boolean isSelect) {
        for (int i = 0; i < shippingAddresses.size(); i++) {
            if (position == i) {
                shippingAddresses.get(i).setIsSelect(isSelect);
            } else {
                shippingAddresses.get(i).setIsSelect(false);
            }
        }
    }
}
